﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using taigu.WeChat;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Microsoft.Extensions.Configuration;
using taigu.WebChat;
using Microsoft.AspNetCore.Authorization;
using taigu.utils;

namespace taigu
{
    public partial class AdminService : ApplicationService, IAdminService
    {

        /// <summary>
        /// 后台帖子管理类
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, Route("/admin/community/GetAwardList")]
        [AllowAnonymous]
        public ResData<object> GetAwardList(AwardInfoAdminDto dto)
        {
            try
            {
                var list = (from awards in _awards
                             join   awards_log in _draw_log on awards.Id equals awards_log.awards_id 
                        // on awards.Id.ToString() equals awards_log.awards_id
                             join  user in _weChatUsersRepository  on awards_log.user_id equals user.Id
                              join address in _addresses on user.Id equals address.user_id
                            // where awards_log.user_id == user.Id.ToString() && awards.Id.ToString() == awards_log.awards_id
                            select new
                             {
                                user_id=user.Id,
                                nickName=user.nick_name,
                                avatarUrl=user.avatar_url,
                                realName = user.real_name,
                                 drawId = awards.Id,
                                 awardName = awards.name,
                                 awardImageUrl =imgHost+ awards.image_url,
                                 awardGrade = awards.grade,
                                 createTime = awards_log.CreationTime,
                                address=address.address,
                                addressPhone= address.phone,
                                addressName= address.name
                             });
                list = list.Where(x => x.awardGrade < 4);

                if (dto.grade >=1)
                {
                    list = list.Where(x => x.awardGrade==dto.grade);
                }
                

                //if (dto.orderBy == "likes")
                //{
                //    if (dto.order == DataOrderType.asc)
                //    {
                //        list = list.OrderByDescending(x => x.post_order).ThenBy(x => x.likesCount).ThenByDescending(x => x.createTime);
                //    }
                //    else
                //    {
                //        list = list.OrderByDescending(x => x.post_order).ThenByDescending(x => x.likesCount).ThenByDescending(x => x.createTime);
                //    }
                //}
                //if (dto.orderBy == "date")
                //{
                //    if (dto.order == DataOrderType.asc)
                //    {
                //        list = list.OrderByDescending(x => x.post_order).ThenBy(x => x.createTime);
                //    }
                //    else
                //    {
                //        list = list.OrderByDescending(x => x.post_order).ThenByDescending(x => x.createTime);
                //    }
                //}

                int count = list.Count();

                if (dto.page < 0)
                    dto.page = 1;
                if (dto.pageSize < 0)
                    dto.pageSize = 10;
                list = list.Skip((dto.page - 1) * dto.pageSize).Take(dto.pageSize);


                return new ResData<object>() { Status = ResStatus.成功, Msg = "sucess", Info = new { count = count, list = list.ToList() } };
            }
            catch (Exception ex)
            {
                return new ResData<object>() { Status = ResStatus.查询失败, Msg = ex.Message, Info = null };
            }
        }

    }
}
